package figuren;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

public class Polynom extends Kurve
{
		List<String>	polynomArray;

	public Polynom(String p)
	{
		this.polynomArray = new ArrayList<String>();
		this.createPolynomArray(p); // erstelle polynom array
		this.aussenLinie = this.berechneXY();
	}

	public Polynom(List<String> polynomArray)
	{
		this.polynomArray = polynomArray;
		this.aussenLinie = this.berechneXY();
	}

	public void createPolynomArray(String polynomString)
	{
		StringTokenizer tok = new StringTokenizer(polynomString, " ");
		while (tok.hasMoreTokens())
		{
			this.polynomArray.add(tok.nextToken());
		}
		Collections.reverse(this.polynomArray);
	}

	public double berechneY(double x)
	{

		double y = 0;
		for (int i = 0; i < polynomArray.size(); i++)
		{
			y = y + (Double.parseDouble(polynomArray.get(i)) * Math.pow(x, i));
		}

		return y;
	}

	public List<Point> berechneXY()
	{
		List<Point> tabelle = new ArrayList<Point>();

		// alle y fue x (-100 ... +100) berechnen
		for (double x = -25; x < +25; x++)
		{
			double y = this.berechneY(x);
			Point p = new Point(x + 100, y + 400); // Grafikverschiebung (keine negative Werte auf
			// dem Bild vorhanden)
			tabelle.add(p);
		}
		return tabelle;
	}

	public int grad()
	{
		return (this.polynomArray.size() - 1);
	}

	public List<String> getPolynomArray()
	{
		return polynomArray;
	}

	public Polynom plus(Polynom otherPolynom)
	{
		List<String> polyBig, polySmall;

		if (this.grad() > otherPolynom.grad())
		{
			polyBig = this.polynomArray;
			polySmall = otherPolynom.getPolynomArray();
		} else
		{
			polyBig = otherPolynom.getPolynomArray();
			polySmall = this.polynomArray;
		}

		List<String> polyResult = new ArrayList<String>();
		polyResult.addAll(polyBig);

		for (int i = 0; i < polySmall.size(); i++)
		{
			polyResult.set(i, ""
					+ (Double.parseDouble(polyBig.get(i)) + Double.parseDouble(polySmall.get(i))));
		}

		return new Polynom(polyResult);
	}

	public Polynom ableitung()
	{
		List<String> poly = new ArrayList<String>();
		poly.addAll(this.polynomArray);

		for (int i = 0; i < poly.size(); i++)
		{
			poly.set(i, (i * Double.parseDouble(poly.get(i))) + "");
		}

		// jetz ist das polynom um ein grad kleiner geworden
		poly.remove(0);
		return new Polynom(poly);
	}

	public String toString()
	{
		return "Polynom: " + this.polynomArray;
	}

	public static void main(String[] args)
	{

		Polynom p = new Polynom("5 0 3");
		System.out.println(p);

		Polynom p2 = new Polynom("1 3 4");
		System.out.println("Ableitung von " + p2 + " ist " + p2.ableitung());

		/*
		 * // Collections.rotate(list, distance) System.out.println(p.grad());
		 * System.out.println(p.berechneXY());
		 */

		System.out.println("Summe von " + p + " und " + p2 + " ist " + (p.plus(p2)));
	}
}
